//
//  ZQTabbar.swift
//  Sina Blog
//
//  Created by 张强 on 16/5/11.
//  Copyright © 2016年 张强. All rights reserved.
//

import UIKit

class ZQTabbar: UITabBar {
    
    var ComposeClosure: (() -> ())?
    
   private lazy var ComposeBtn: UIButton = {
    let btn = UIButton()
        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        btn.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.setImage(UIImage.init(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.sizeToFit()
        
        btn.addTarget(self, action: #selector(btnClick), forControlEvents: .TouchUpInside)
        
        
        return btn
        
    
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        SetupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func SetupUI(){
    addSubview(ComposeBtn)
        
    
    
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        var index = 0
        
        let itemWidth = frame.size.width / 5
        
        ComposeBtn.center.x = frame.size.width / 2
        ComposeBtn.center.y = frame.size.height / 2
        
        for childView in subviews {
            
            
            if childView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                childView.frame.size.width = itemWidth
                childView.frame.origin.x = CGFloat(index) * itemWidth
                index += 1
                if index == 2 {
                    index += 1
                    
                }
            }
            
        }
        
    }
    
    
    
    
    
    
    
    
   @objc private func btnClick() {
    
        ComposeClosure!()
    
    }
    
    
    


}
